local function TIPS(content, color)
    TIP("自动池钓", color or "green", content)
end

local BufferedAction = GLOBAL.BufferedAction
local ACTIONS = GLOBAL.ACTIONS
local SendRPCToServer = GLOBAL.SendRPCToServer
local RPC = GLOBAL.RPC
local ACTIONS = GLOBAL.ACTIONS
local thread
local id = "hx_autofishing"
local fishingrod = "fishingrod"
local Sleep = GLOBAL.Sleep

local function StopThread(message)
    GLOBAL.KillThreadsWithID(id)
    thread = nil
    TIPS(message or "任务终止", "red")
end
local function getRod()
    local rod = GetEquippedItemFrom(GLOBAL.EQUIPSLOTS.HANDS)
    if not rod or not rod:HasTag(fishingrod) then
        rod = GetItemFromAll(nil, {fishingrod})
        -- GLOBAL.ThePlayer.replica.inventory:UseItemFromInvTile(rod)
        local inventory = GLOBAL.ThePlayer.components.inventory
        if inventory then
            inventory:EquipActionItem(rod)
        else
            -- SendRPCToServer(RPC.EquipActionItem, rod)
            SendRPCToServer(RPC.UseItemFromInvTile, ACTIONS.EQUIP.code, rod)
        end
    end
    return rod
end
local function tryRepair(player, controller, rod)
    if not player or not controller or not rod then
        return
    end
    local perc = GetPerc(rod)
    if perc < 1 and rod.prefab == "medal_fishingrod" then
        local silk = GetItemFromAll("silk")
        if silk then
            local repair_act = ACTIONS.REPAIRCOMMON
            if type(repair_act) == "table" then
                local buff_act = BufferedAction(player, rod, repair_act, silk)
                buff_act.preview_cb = function()
                    SendRPCToServer(RPC.ControllerUseItemOnItemFromInvTile, repair_act.code, rod, silk, repair_act.mod_name)
                end
                if controller.locomotor then
                    controller:DoAction(buff_act)
                else
                    buff_act.preview_cb()
                end
                Sleep(0.5)
                TIPS(silk.name .. "修复" .. rod.name .. "  " .. GetPerc(rod) .. "%", "pink")
            end
        end
    end
end
local allowString = {GLOBAL.STRINGS.ACTIONS.FISH, GLOBAL.STRINGS.ACTIONS.REEL.REEL, GLOBAL.STRINGS.ACTIONS.REEL.GENERIC}
local function fn()
    local player = GLOBAL.ThePlayer
    local builder = player.replica.builder
    local inventory = player.replica.inventory
    local picker = player.components.playeractionpicker
    local controller = player.components.playercontroller
    if thread or not builder or not inventory or not picker or not controller then
      StopThread("停止")
      return
    end
    local pond = PlayerFindEnt(nil, nil, {"fishable"})
    local pos
    local recipe = GLOBAL.GetValidRecipe(fishingrod)

    if not pond then
        return TIPS("周围没有池塘", "red")
    else
        pos = pond:GetPosition()
        TIPS("开始")
    end
    thread = GLOBAL.StartThread(function()
        while thread do
            Sleep(0.25)
            local rod = getRod()
            if not rod then
                if builder:HasIngredients(recipe) and (builder:KnowsRecipe(recipe) or GLOBAL.CanPrototypeRecipe(recipe.level, builder:GetTechTrees())) then
                    TIPS("尝试制作鱼竿")
                    builder:MakeRecipeFromMenu(recipe)
                    Sleep(3)
                    rod = getRod()
                end
            end
            if not rod then
                StopThread("缺少鱼竿, 任务终止")
                break
            else
                tryRepair(player, controller, rod)
                local fish_act
                local actions = picker:GetEquippedItemActions(pond, rod)
                for _, action in pairs(actions)do
                    if table.contains({ACTIONS.FISH, ACTIONS.REEL}, action.action)then
                        fish_act = action
                    end
                end
                if fish_act and table.contains(allowString, fish_act:GetActionString()) then
                    fish_act.preview_cb = function()
                        SendRPCToServer(RPC.LeftClick, fish_act.action.code, pos.x, pos.z, pond, false, 10)
                    end
                    if controller.locomotor then 
                        controller:DoAction(fish_act)
                    else
                        fish_act.preview_cb()
                    end
                end
            end
        end
    end, id)
end

InterruptedByMobile(function() return thread end, StopThread)

if GetModConfigData("sw_autofish") == "biubiu" then
    DEAR_BTNS:AddDearBtn(GLOBAL.GetInventoryItemAtlas("fishingrod.tex"), "fishingrod.tex", "自动池钓", "钓池塘里的鱼 (新版本)", false, fn)
end
    
AddBindBtn("sw_autofish", fn)